from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from django.core.cache import cache
from home import models, homeser


# Create your views here.
class Header(APIView):
    def get(self, request):
        navdata = models.Nav.objects.filter(position=1, isdel=False).order_by('sort')
        ser = homeser.NavSerializer(navdata, many=True)
        return Response({"msg": "ok", "code": 200, "data": ser.data})


class Footer(APIView):
    def get(self, request):
        navdata = models.Nav.objects.filter(position=0, isdel=False).order_by('sort')
        ser = homeser.NavSerializer(navdata, many=True)
        return Response({"msg": "ok", "code": 200, "data": ser.data})


class Banner(APIView):
    def get(self, request):
        bannerdata = cache.get('banner')
        if bannerdata:
            return Response({"msg": "ok", "code": 200, "data": bannerdata})
        navdata = models.Banner.objects.filter(isdel=False).order_by('sort')
        ser = homeser.BannerSerializer(navdata, many=True)
        cache.set('banner', ser.data, 60 * 60 * 24)
        return Response({"msg": "ok", "code": 200, "data": ser.data})
